package com.ldoublem.loadingviewlib.view;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.ldoublem.loadingviewlib.view.base.LVBase;


public class LVCircularJump extends LVBase implements Component.EstimateSizeListener, Component.DrawTask {

    private Paint mPaint;

    private float mWidth = 0f;
    private float mHigh = 0f;
    private float mMaxRadius = 6;
    private int circularCount = 4;
    private float mAnimatedValue = 0f;
    private int mJumpValue = 0;

    public LVCircularJump(Context context) {
        super(context);
    }

    public LVCircularJump(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    public LVCircularJump(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }







    private void initPaint() {
        setEstimateSizeListener(this);
        addDrawTask(this);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setColor(Color.WHITE);

    }

    public void setViewColor(int color)
    {
        mPaint.setColor(new Color(color));
        invalidate();
    }

    @Override
    protected void InitPaint() {
        initPaint();
    }

    @Override
    protected void OnAnimationUpdate(AnimatorValue valueAnimator, float v) {
        mAnimatedValue = v;


        if (mAnimatedValue > 0.5) {
            mAnimatedValue = 1 - mAnimatedValue;
        }

        invalidate();
    }





    @Override
    protected void OnAnimationRepeat(Animator animation) {
        mJumpValue++;
    }


    @Override
    protected int SetAnimRepeatMode() {
        return RESTART;
    }

    @Override
    protected int OnStopAnim() {
        mAnimatedValue = 0f;
        mJumpValue = 0;
        return 0;
    }
    @Override
    protected void AinmIsRunning() {

    }
    @Override
    protected int SetAnimRepeatCount() {
        return INFINITE;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        canvas.save();
        float circularX = mWidth / circularCount;

        for (int i = 0; i < circularCount; i++) {

            if (i == mJumpValue % circularCount) {

                canvas.drawCircle(i * circularX + circularX / 2f,
                        mHigh / 2 - mHigh / 2 * mAnimatedValue,
                        mMaxRadius, mPaint);

            } else {
                canvas.drawCircle(i * circularX + circularX / 2f,
                        mHigh / 2,
                        mMaxRadius, mPaint);
            }

        }
        canvas.restore();
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        mWidth = EstimateSpec.getSize(i);
        mHigh = EstimateSpec.getSize(i1);
        return false;
    }
}
